<?php

class html5av_audio_video {

    private $ID = false;
    private $type;
    private $poster_path;
    private $poster_filename;
    private $width;
    private $height;
    private $view;
    private $title;
    private $alt = "...";
    private $author_id;
    private $created_on;
    private $modified;

    public function getID() {
        return (int) $this->ID;
    }

    public function getType() {
        return $this->type;
    }

    public function setType($type) {
        $this->type = $type;
    }

    public function getPosterPath() {
        return $this->poster_path;
    }

    public function setPosterPath($poster_path) {
        $this->poster_path = $poster_path;
    }

    public function getPosterFilename() {
        return $this->poster_filename;
    }

    public function setPosterFilename($poster_filename) {
        $this->poster_filename = $poster_filename;
    }

    public function getWidth() {
        return (int) $this->width;
    }

    public function setWidth($width) {
        if (is_numeric($width))
            $this->width = $width;
    }

    public function getHeight() {
        return (int) $this->height;
    }

    public function setHeight($height) {
        if (is_numeric($height))
            $this->height = $height;
    }

    public function getView() {
        if ($this->view != '') {
            return $this->view;
        } else {
            return false;
        }
    }

    public function setView($view) {
        $this->view = $view;
    }

    public function getTitle() {
        return $this->title;
    }

    public function setTitle($title) {
        $this->title = $title;
    }

    public function getAlt() {
        return $this->alt;
    }

    public function setAlt($alt) {
        if($alt != '')
            $this->alt = $alt;
    }

    public function getAuthorId() {
        return (int) $this->author_id;
    }

    public function setAuthorId() {
        $current_user = wp_get_current_user();
        $this->author_id = (int) $current_user->ID;
    }

    public function getCreatedOn() {
        return html5av_time::utcToCurZone($this->created_on);
    }

    public function setCreatedOn() {
        $this->created_on = html5av_time::getUTC();
    }

    public function getModified() {
        return html5av_time::utcToCurZone($this->modified);
    }

    public function setModified() {
        $this->modified = html5av_time::getUTC();
    }

    public function getSources($incDisabled = false) {
        return html5av_source_table::getByAudioVideoId($this->ID, $incDisabled);
    }

    public function getTracks($incDisabled = false) {
        return html5av_track_table::getByAudioVideoId($this->ID, $incDisabled);
    }

    public function getPoster() {
        return html5av_manager::getUploadDir() . '/' .
        $this->getPosterPath() . '/' . $this->getPosterFilename();
    }

    public function getPosterUrl() {
        return html5av_manager::getUploadUrl() . '/' .
        $this->getPosterPath() . '/' . $this->getPosterFilename();
    }

    public function getAuthorName() {
        $user_info = get_userdata($this->getAuthorId());
        return $user_info->display_name;
    }

    public function save() {
        $attr = html5av_dbal::toArray($this, __CLASS__); 
        $db = new html5av_dbal();
        $this->ID = $db->save($this->ID, $attr, 'audio_video');
    }

    public function delete() {
        if ($this->ID) {
            $sources = $this->getSources(true);
            $tracks = $this->getTracks(true);
            foreach ($sources as $source) {
                $source->delete();
            }
            foreach ($tracks as $track) {
                $track->delete();
            }
            unlink($this->getPoster());
            $db = new html5av_dbal();
            $this->ID = $db->delete($this->ID, 'audio_video');
        }
    }

}

class html5av_audio_video_table {

    static public function getByID($id) {
        $db = new html5av_dbal();
        $sql = "SELECT * FROM `" . $db->getPrefix() . "audio_video` WHERE `ID`=?;";
        $prepare = $db->prepare($sql);
        $prepare->execute(array($id));
        return $prepare->fetchObject('html5av_audio_video');
    }

    static public function pager() {
        $db = new html5av_dbal();
        $sql = "SELECT count(*) as `COUNT` FROM `" . $db->getPrefix() . "audio_video`";
        $query = $db->query($sql);
        if ($query) {
            $data = $query->fetch();
            $count = (int) $data['COUNT'];
        } else {
            //Failsafe for when SQL count() fails
            $count = self::getCount();
        }
        $lastPage = (int) ceil($count / 25);
        $array = array(
            1,
            $lastPage
        );
        return $array;
    }

    static public function getPage($page) {
        $db = new html5av_dbal();
        $offset = ($page - 1) * 25;
        $sql = "SELECT * FROM `" . $db->getPrefix() . "audio_video` ORDER BY `ID` DESC
            LIMIT " . $offset . ", 25;";
        $prepare = $db->prepare($sql);
        $prepare->execute();
        $objects = array();
        while ($object = $prepare->fetchObject('html5av_audio_video')) {
            $objects[] = $object;
        }
        return $objects;
    }
    
    static private function getCount() {
        $db = new html5av_dbal();
        $sql = "SELECT * FROM `" . $db->getPrefix() . "audio_video`;";
        $prepare = $db->prepare($sql);
        $prepare->execute();
        return (int) count($prepare->fetchAll());
    }

}
